package com.college.banner.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.college.banner.entity.vo.BannerQuery;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.college.banner.mapper.CrmBannerMapper;
import com.college.banner.entity.CrmBanner;
import com.college.banner.service.CrmBannerService;
import org.springframework.util.StringUtils;

@Service
public class CrmBannerServiceImpl extends ServiceImpl<CrmBannerMapper, CrmBanner> implements CrmBannerService{

    @Override
    public void pageQuery(Page<CrmBanner> bannerPage, BannerQuery bannerQuery) {
        QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
        String title = bannerQuery.getName();
        String begin = bannerQuery.getBegin();
        String end = bannerQuery.getEnd();
        //判空,构造查询条件
        if(StringUtils.hasText(title)){
            wrapper.like("title",title);
        }
        if(StringUtils.hasText(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if(StringUtils.hasText(end)){
            wrapper.le("gmt_modified",end);
        }
        wrapper.orderByDesc("gmt_create");
        //进行分页查询
        this.baseMapper.selectPage(bannerPage, wrapper);//分页查询后自动封装到page中
    }

    @Override
    //@Cacheable用于查询
    @Cacheable(value = "banner", key = "'selectIndexList'")
    public List<CrmBanner> getlist() {
        QueryWrapper<CrmBanner> wrapper=new QueryWrapper();
        wrapper.last("limit 3");
        List<CrmBanner> list=baseMapper.selectList(wrapper);
        return list;
    }
    @CacheEvict(value = "banner", allEntries=true)
    @Override
    public void saveBanner(CrmBanner banner) {
        baseMapper.insert(banner);
    }

    //CacheEvict用于更新或删除，allEntries属性清楚缓存
    @CacheEvict(value = "banner", allEntries=true)
    @Override
    public void updateBannerById(CrmBanner banner) {
        baseMapper.updateById(banner);
    }

    //CacheEvict用于更新或删除，allEntries属性清楚缓存
    @CacheEvict(value = "banner", allEntries=true)
    @Override
    public void removeBannerById(String id) {
        baseMapper.deleteById(id);
    }
}
